package com.java.恋上算法.数据结构._06_二叉搜索树;

import com.java.恋上算法.数据结构._06_二叉搜索树.BinarySearchTree.Visitor;
import com.java.恋上算法.数据结构._06_二叉搜索树.file.Files;
import com.java.恋上算法.数据结构._06_二叉搜索树.printer.BinaryTreeInfo;
import com.java.恋上算法.数据结构._06_二叉搜索树.printer.BinaryTrees;
import java.util.Comparator;

/**
 * @desc: xxx
 * @author: zhanghongqiang01@baijiahulian.com
 * @date: 2021/9/9/0009 下午 06:01
 */
public class Main {

    private static class PersonComparator implements Comparator<Person> {
        @Override
        public int compare(Person e1, Person e2) {
            return e1.getAge() - e2.getAge();
        }
    }

    private static class PersonComparator2 implements Comparator<Person> {
        @Override
        public int compare(Person e1, Person e2) {
            return e2.getAge() - e1.getAge();
        }
    }

    static void test(){
        /*
         * Java的匿名类，类似于iOS中的Block、JS中的闭包（function）
         */

		BinarySearchTree<Person> bst1 = new BinarySearchTree<>(new Comparator<Person>() {
			@Override
			public int compare(Person o1, Person o2) {
				return 0;
			}
		});
		bst1.add(new Person(12));
		bst1.add(new Person(15));

		BinarySearchTree<Person> bst2 = new BinarySearchTree<>(new PersonComparator());
		bst2.add(new Person(12));
		bst2.add(new Person(15));


		//BinarySearchTree<Car> bst4 = new BinarySearchTree<>(new Car);


		java.util.Comparator<Integer> iComparator;
    }

    static void test1() {
        Integer data[] = new Integer[] {
                7, 4, 9, 2, 5, 8, 11, 3, 12, 1
        };

        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            bst.add(data[i]);
        }

        BinaryTrees.println(bst);
    }

    static void test2() {
        Integer data[] = new Integer[] {
                7, 4, 9, 2, 5, 8, 11, 3, 12, 1
        };

        BinarySearchTree<Person> bst1 = new BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            bst1.add(new Person(data[i]));
        }

        BinaryTrees.println(bst1);

        BinarySearchTree<Person> bst2 = new BinarySearchTree<>(new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o2.getAge() - o1.getAge();
            }
        });
        for (int i = 0; i < data.length; i++) {
            bst2.add(new Person(data[i]));
        }
        BinaryTrees.println(bst2);
    }

    static void test3() {
        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (int i = 0; i < 40; i++) {
            bst.add((int)(Math.random() * 100));
        }
//         BinaryTrees.println(bst);

        String str = BinaryTrees.printString(bst);
        str += "\n";
        Files.writeToFile("F:/1.txt", str, true);


    }

    static void test4() {
        BinaryTrees.println(new BinaryTreeInfo() {

            @Override
            public Object string(Object node) {
                return node.toString() + "_";
            }

            @Override
            public Object root() {
                return "A";
            }

            @Override
            public Object right(Object node) {
                if (node.equals("A")) {
                    return "C";
                }
                if (node.equals("C")) {
                    return "E";
                }
                return null;
            }

            @Override
            public Object left(Object node) {
                if (node.equals("A")) {
                    return "B";
                }
                if (node.equals("C")) {
                    return "D";
                }
                return null;
            }
        });
    }

    static void test5() {
        BinarySearchTree<Person> bst = new BinarySearchTree<>();
        bst.add(new Person(10, "jack"));
        bst.add(new Person(12, "rose"));
        bst.add(new Person(6, "jim"));

        bst.add(new Person(10, "michael"));

        BinaryTrees.println(bst);
    }

    static void test6() {
        Integer data[] = new Integer[] {
                7, 4, 9, 2, 5
        };

        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            bst.add(data[i]);
        }
        BinaryTrees.println(bst);
//        bst.postorderTraversal();

//		BinarySearchTree<Integer> bst = new BinarySearchTree<>();
//		for (int i = 0; i < 10; i++) {
//			bst.add((int)(Math.random() * 100));
//		}
//        BinaryTrees.println(bst);
//        System.out.println(bst.isComplete());
//
//        bst.levelOrderTraversal();

		/*/
		 *       7
		 *    4    9
		    2   5
		 */

//		bst.levelOrder(new Visitor<Integer>() {
//			@Override
//            public void visit(Integer element) {
//				System.out.print("_" + element + "_ ");
//			}
//		});
//
//		bst.inorder(new Visitor<Integer>() {
//			@Override
//            public void visit(Integer element) {
//				System.out.print("_" + (element + 3) + "_ ");
//			}
//		});
//
//         System.out.println(bst.height());
    }

    static void test7() {
        Integer data[] = new Integer[] {
                7, 4, 9, 2, 5, 8, 11, 3, 12, 1
        };

        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            bst.add(data[i]);
        }

        BinaryTrees.println(bst);

        bst.remove(7);

        BinaryTrees.println(bst);
    }

    /*static void test8() {
        Integer data[] = new Integer[] {
                7, 4, 9, 2, 1
        };

        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            bst.add(data[i]);
        }
        BinaryTrees.println(bst);
        System.out.println(bst.isComplete());
    }*/

    static void test9() {
        Integer data[] = new Integer[] {
                7, 4, 9, 2, 1
        };

        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            bst.add(data[i]);
        }
        BinaryTrees.println(bst);

        bst.preorder(new Visitor<Integer>() {
            @Override
            public boolean visit(Integer element) {
                System.out.print(element + " ");
                return element == 2 ? true : false;
            }
        });
        System.out.println();

        bst.inorder(new Visitor<Integer>() {
            @Override
            public boolean visit(Integer element) {
                System.out.print(element + " ");
                return element == 4 ? true : false;
            }
        });
        System.out.println();

        bst.postorder(new Visitor<Integer>() {
            @Override
            public boolean visit(Integer element) {
                System.out.print(element + " ");
                return element == 4 ? true : false;
            }
        });
        System.out.println();

        bst.levelOrder(new Visitor<Integer>() {
            @Override
            public boolean visit(Integer element) {
                System.out.print(element + " ");
                return element == 9 ? true : false;
            }
        });
        System.out.println();
    }

    public static void main(String[] args) {

        test6();

    }

}
